package cn.bookCity.filter;

import java.io.IOException;
import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet Filter implementation class UserFilter
 */
@WebFilter(
		dispatcherTypes = {
				DispatcherType.REQUEST, 
				DispatcherType.FORWARD
		}
					, 
		urlPatterns = { 
				"/user/*", 
				"/manager/*"
		})
public class UserFilter implements Filter {

	  public UserFilter() {
	        // TODO Auto-generated constructor stub
	    }

		
		public void destroy() {
			// TODO Auto-generated method stub
		}

		
		public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
			HttpServletRequest req=(HttpServletRequest)request;
			 String servletPath = req.getServletPath();
//			 System.out.println(111);
			 if ("/user/msg.jsp".equals(servletPath) ||"/user/login.jsp".equals(servletPath) || "/jsps/user/signup.jsp".equals(servletPath) || "/jsps/user/resetpassword.jsp".equals(servletPath)|| "/resetpasswordafter.jsp".equals(servletPath) || "/login".equals(servletPath) || servletPath.contains("/images/") || servletPath.contains("/js/") || servletPath.contains("/My97DatePicker/") || servletPath.contains("/styles/")||servletPath.contains("EmailServlet") ) {
		            chain.doFilter(req, response);
		        } else {
		            Object loginUser = req.getSession().getAttribute("session_user");
		            if (loginUser == null) {
		            	((HttpServletResponse) response).sendRedirect(req.getContextPath() + "/jsps/user/login.jsp");
		            } else {
		                chain.doFilter(req, response);
		            }
		        }
		}

		
		public void init(FilterConfig fConfig) throws ServletException {
			// TODO Auto-generated method stub
		}

}
